/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics;

import czStvDpds.input.LWJGLKeyEvent;
import czStvDpds.input.LWJGLMouseEvent;
import czStvDpds.logging.Logging;
import czStvDpds.mapmanagment.AccesibleMap;
import czStvDpds.movement.AbstractPlayer;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.lwjgl.opengl.Display;

/**
 *
 * @author divad
 */
class GamePane extends JPanel implements MouseListener, MouseMotionListener, KeyListener {

    private int canvasWidth = 640, canvasHeight = 320;
    private Drawing2D gameWindow;
    private AccesibleMap map;
    private BufferedImage paintedMap;
    private int mX, mY;

    /**
     * vytvoří nový GamePane (herní výplň okna)
     */
    public GamePane() {
        this.gameWindow = GameWindow.getDrawing();
//        this.addMouseListener(this);
    }

    @Override
    public Dimension getMaximumSize() {
        return getRozmer();
    }

    @Override
    public Dimension getMinimumSize() {
        return getRozmer();
    }

    @Override
    public Dimension getPreferredSize() {
        return getRozmer();
    }

    private Dimension getRozmer() {
        return new Dimension(this.canvasWidth, this.canvasHeight);
    }

////////////////////// - kresleni - /////////////////////////
    /**
     * vykreslí hru
     *
     * @param g1
     */
    @Override
    public void paint(java.awt.Graphics g1) {
        Graphics2D g = (Graphics2D) g1;

//        Map2D map = GameWindow.getDrawing().getMap();
        if (this.map == null && GameWindow.getDrawing().getMap() != this.map) {
            this.map = GameWindow.getDrawing().getMap();
//            this.canvasHeight = map.getTileSize() * map.getHeight();
//            this.canvasWidth = map.getTileSize() * map.getWidth();
            this.canvasHeight = 25 * map.getHeight();
            this.canvasWidth = 25 * map.getWidth();
            this.paintedMap = new BufferedImage(this.canvasWidth, this.canvasHeight, BufferedImage.TYPE_INT_RGB);
            this.paintMap((Graphics2D) paintedMap.getGraphics());
            GameWindow.getDrawing().refresh();
        }
        if (this.paintedMap != null) {
            this.paintEnviroment(g);
        }
        this.paintEntities(g);
    }

    /**
     * vykreslí mapu do obrazu
     *
     * @param g
     */
    private void paintMap(Graphics2D g) {
//        int tileSize = map.getTileSize();
        int tileSize = 25;
        for (int y = 0; y < this.map.getHeight(); y++) {
            for (int x = 0; x < this.map.getWidth(); x++) {
                if (map.getTile(x, y).getId() == 9) {
                    g.setColor(Color.black);
                } else if (map.getTile(x, y).getId() == 8) {
                    g.setColor(new Color(160, 87, 5));
                } else if (map.getTile(x, y).getId() == 7) {
                    g.setColor(new Color(251, 56, 0));
                } else if (map.getTile(x, y).getId() == 6) {
                    g.setColor(new Color(116, 253, 108));
                } else if (map.getTile(x, y).getId() == 5) {
                    g.setColor(new Color(180, 187, 174));
                } else if (map.getTile(x, y).getId() == 4) {
                    g.setColor(new Color(222, 78, 41));
                } else if (map.getTile(x, y).getId() == 3) {
                    g.setColor(new Color(24, 100, 2));
                } else if (map.getTile(x, y).getId() == 2) {
                    g.setColor(new Color(35, 143, 3));
                } else {
                    g.setColor(Color.green);
                }
                g.fillRect(x * tileSize, y * tileSize, tileSize, tileSize);
            }
        }
    }

    /**
     * vykreslí mapu do okna
     *
     * @param g
     */
    private void paintEnviroment(Graphics2D g) {
        g.drawImage(this.paintedMap, null, 0, 0);
    }

    /**
     * vykreslí hráče do okna
     *
     * @param g
     */
    private void paintEntities(Graphics2D g) {
        ArrayList<AbstractPlayer> players = GameWindow.getDrawing().getPlayers();
        for (AbstractPlayer player : players) {
            ((VisiblePlayer2D) player).paint(g);
        }
    }
////////////////// - listenery - /////////////////////

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
//        System.out.println("mouse entered");
        try {
//            new Robot().mouseMove(Display.getX()+(Display.getWidth()/2), Display.getY()+(Display.getHeight()/2));
            new Robot().mouseMove((int) (this.getLocationOnScreen().getX() + (this.getWidth() / 2)), (int) (this.getLocationOnScreen().getY() + (this.getHeight() / 2)));
            this.mX = (int) (this.getLocationOnScreen().getX() + (this.getWidth() / 2));
            this.mY = (int) (this.getLocationOnScreen().getY() + (this.getHeight() / 2));
        } catch (AWTException ex) {
//            Logger.getLogger(GamePane.class.getName()).log(Level.SEVERE, null, ex);
            Logging.getMainLogger().log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
//        System.out.println("mouse moved");
        ArrayList<AbstractPlayer> players = GameWindow.getDrawing().getPlayers();

        LWJGLMouseEvent me = new LWJGLMouseEvent(0, false, e.getX() - mX, e.getY() - mY, e.getX(), e.getY(), 0, -1);


        try {
            new Robot().mouseMove((int) (this.getLocationOnScreen().getX() + (this.getWidth() / 2)), (int) (this.getLocationOnScreen().getY() + (this.getHeight() / 2)));
        } catch (AWTException ex) {
            Logger.getLogger(GamePane.class.getName()).log(Level.SEVERE, null, ex);
        }


        for (AbstractPlayer player : players) {
            ((VisiblePlayer2D) player).mouseAction(me);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        ArrayList<AbstractPlayer> players = GameWindow.getDrawing().getPlayers();
        for (AbstractPlayer player : players) {
//            player.handleMousePress(e);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        ArrayList<AbstractPlayer> players = GameWindow.getDrawing().getPlayers();
        for (AbstractPlayer player : players) {
//            player.handleMouseRelease(e);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
//        System.out.println("key pressed");
        ArrayList<AbstractPlayer> players = GameWindow.getDrawing().getPlayers();
        LWJGLKeyEvent le = null;
        if (e.getKeyCode() == KeyEvent.VK_W) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_W, 'w', true, -1);
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_S, 's', true, -1);
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_S, 'a', true, -1);
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_S, 'd', true, -1);
        }

        if (le != null) {
            for (AbstractPlayer player : players) {
                ((VisiblePlayer2D) player).keyAction(le);
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
//        System.out.println("key released");
        ArrayList<AbstractPlayer> players = GameWindow.getDrawing().getPlayers();
        LWJGLKeyEvent le = null;
        if (e.getKeyCode() == KeyEvent.VK_W) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_W, 'w', false, -1);
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_S, 's', false, -1);
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_S, 'a', false, -1);
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            le = new LWJGLKeyEvent(LWJGLKeyEvent.KEY_S, 'd', false, -1);
        }

        if (le != null) {
            for (AbstractPlayer player : players) {
                ((VisiblePlayer2D) player).keyAction(le);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
}
